import Toast from "./Toast";

const plugin = {}

plugin.install = (vue) => {
  //1.创建Toast组件的构造器
  const toastConstructor = vue.extend(Toast)

  toastConstructor.prototype.close = function (){
    this.isShow = false
    this.$el.addEventListener('transitionend',removeDown)
    console.log(this.$el);
  }

  vue.prototype.$toast =(options={})=>{
    //1.new的方式，根据组件构造函，可以创建出来一个组件对象
    const toast = new toastConstructor();
    //2.将组件对象，手动挂载到某一个元素上
    toast.$mount(document.createElement('div'))

    //3.将toast的$el添加到body中
    document.body.appendChild(toast.$el)

    //4.获取用户自定义数据
    const duration = options.duration || 2000
    toast.message = options.message
    toast.isShow = true

    //5.定时器让toast消失
    setTimeout(()=>{
      toast.close()
    },duration)
  }
}

function removeDown(event){
  console.log('removeDown111');
  if(event.target.parentNode){
    event.target.parentNode.removeChild(event.target)
  }
}
export default plugin
